package com.zl.learning.io.nio;

import org.junit.Test;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class ChannelSimple {
    @Test
    public void test1(){
        try (RandomAccessFile accessFile = new RandomAccessFile("conf/in.json","rw")) {
            FileChannel inChannel = accessFile.getChannel();
            ByteBuffer buf = ByteBuffer.allocate(48);
            int bytesRead = inChannel.read(buf);
            while (bytesRead!=-1){
//                System.out.println("read"+bytesRead);
                buf.flip();
                while (buf.hasRemaining()){
                    System.out.print((char)buf.get());
                }
                buf.clear();
                bytesRead = inChannel.read(buf);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
